import request from '@/router/axios';

const gSysDict = {
};

export const getList = (current, size, params) => {
  return request({
    url: '/api/blade-system/dict/list',
    method: 'get',
    params: {
      ...params,
      current,
      size,
    }
  })
}

//获取字典
export const getDictList = (params) =>{
  return request({
    url:'/api/blade-system/dict/list',
    method:'get',
    params:{
      code:params
    }
  })
}

//获取字典
export const getDictListSet = (codeList) =>{
  return request({
    url:'/api/blade-system/dict/dictionaryList',
    method:'get',
    params:{
      codeList
    }
  })
}

export const getParentList = (current, size, params) => {
  return request({
    url: '/api/blade-system/dict/parent-list',
    method: 'get',
    params: {
      ...params,
      current,
      size,
    }
  })
}

export const getChildList = (current, size, parentId, params) => {
  return request({
    url: '/api/blade-system/dict/child-list',
    method: 'get',
    params: {
      ...params,
      current,
      size,
      parentId: parentId,
    }
  })
}

export const remove = (ids) => {
  return request({
    url: '/api/blade-system/dict/remove',
    method: 'post',
    params: {
      ids,
    }
  })
}

export const add = (row) => {
  return request({
    url: '/api/blade-system/dict/submit',
    method: 'post',
    data: row
  })
}

export const update = (row) => {
  return request({
    url: '/api/blade-system/dict/submit',
    method: 'post',
    data: row
  })
}


export const getDict = (id) => {
  return request({
    url: '/api/blade-system/dict/detail',
    method: 'get',
    params: {
      id,
    }
  })
}
export const getDictTree = () => {
  return request({
    url: '/api/blade-system/dict/tree?code=DICT',
    method: 'get'
  })
}

export const getDictionary = (params) => {
  return request({
    url: '/api/blade-system/dict/dictionary',
    method: 'get',
    params,
  })
}

export const getDesigner = () =>{
  return request({
    url:'/api/blade-user/get-designer-select',
    method:'get'
  })
}

export const getGeoList = () =>{
  return request({
    url:'/api/blade-system/geo/get',
    method:'get',
  })
}

export const getUrlData = (url, params = {}) => {
  return request({
    url: url,
    method: 'get',
    params,
  })
}

export const getSysDict = (code) => {
  if(gSysDict[code]) {
    return new Promise(function(resolve, reject){
      resolve(gSysDict[code]);
    });
  } else {
    return request({
      url: '/api/blade-system/dict/dictionary',
      method: 'get',
      params: {
        code
      }
    }).then(res=>{
      gSysDict[code] = res.data.data;
      return gSysDict[code];
    })
  }
};

export const getSysDictList = (codes) => {
  let codeList = codes.split(',');
  let resultList = {};
  let newCodeList = [];
  codeList.forEach(code=>{
    if(!gSysDict[code]) {
      newCodeList.push(code);
    } else {
      resultList[code] = gSysDict[code];
    }
  });
  if(newCodeList.length == 0) {
    return new Promise(function(resolve, reject){
      resolve(resultList);
    });
  } else {
    return request({
      url: '/api/blade-system/dict/dictionaryList',
      method: 'get',
      params: {
        codeList: newCodeList.join(',')
      }
    }).then(res=>{
      if(res.data != null && res.data.data != null) {
        let listMap = res.data.data;
        newCodeList.forEach(code=>{
          if(listMap[code]) {
            gSysDict[code] = listMap[code];
            resultList[code] = gSysDict[code];
          }
        });
      }
      return resultList;
    });
  }
};
